<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>人脸检测</title>
    <link rel="stylesheet" type="text/css" href="../../bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../css/face/detect_face.css">
</head>
<body>
    <div class="text-center">
        <h1>人脸检测</h1>
    </div>
    <div class="vc-div">
        <video id="video" controls></video>
        <canvas id="canvas" width="350" height="236"></canvas>
    </div>
    <div>
        <button id="capture" class="center-block btn btn-primary">拍照</button>
    </div>
    <div class="container div-container">
        <div id="attr" class="row form-inline">
            <div class="col-md-2 input-group input-group-sm">
                <span class="input-group-addon">年龄</span>
                <input  id="age" type="text" class="form-control">
            </div>
            <div class="col-md-2 col-md-offset-1 input-group input-group-sm">
                <span class="input-group-addon">性别</span>
                <input id="sex" type="text" class="form-control">
            </div>
            <div class="col-md-2 col-md-offset-1 input-group input-group-sm">
                <span class="input-group-addon">微笑程度</span>
                <input id="smile" type="text" class="form-control">
            </div>
            <div class="col-md-2 col-md-offset-1 input-group input-group-sm">
                <span class="input-group-addon">开心程度</span>
                <input id="emotion" type="text" class="form-control">
            </div>
        </div>
    </div>
    <footer class="text-center">
        <a href="https://console.faceplusplus.com.cn/documents/4888373">API参考地址</a><br>
        <a href="http://localhost:8097/">返回主页面</a>
    </footer>
<script src="../../jq/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    //访问用户媒体设备的兼容方法
    function getUserMedia(constraints, success, error) {
        if (navigator.mediaDevices.getUserMedia) {
            //最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        } else if (navigator.webkitGetUserMedia) {
            //webkit核心浏览器
            navigator.webkitGetUserMedia(constraints,success, error)
        } else if (navigator.mozGetUserMedia) {
            //firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        } else if (navigator.getUserMedia) {
            //旧版API
            navigator.getUserMedia(constraints, success, error);
        }
    }

    var video = document.getElementById('video');
    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');

    function success(stream) {
        //兼容webkit核心浏览器
        var CompatibleURL = window.URL || window.webkitURL;
        //将视频流设置为video元素的源
        console.log(stream);

        //video.src = CompatibleURL.createObjectURL(stream);
        video.srcObject = stream;
        video.play();
    }

    function error(error) {
        console.log("访问用户媒体设备失败${error.name}, ${error.message}");
    }

    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
        //调用用户媒体设备, 访问摄像头
        getUserMedia({video : {width: 480, height: 320}}, success, error);
    } else {
        alert('不支持访问用户媒体');
    }

    document.getElementById('capture').addEventListener('click', function () {
        context.drawImage(video, 0, 0, 480, 320);

        var info = {
            imgString: canvas.toDataURL("image/png")
        }

        $.post("http://localhost:8097/face/detect",info,function(data){
            console.log(data);

            $("#sex").val(JSON.stringify(data.faces[0].attributes.gender.value))
            $("#age").val(JSON.stringify(data.faces[0].attributes.age.value))
            $("#smile").val(JSON.stringify(data.faces[0].attributes.smile.value))
            $("#emotion").val(JSON.stringify(data.faces[0].attributes.emotion.happiness))

        },"json")

    })
</script>
</body>
</html>